#include "dbconnectionmanager.h"
#include <STDLIB.H>
DBConnectionManager::DBConnectionManager()
{
}

DBConnectionManager::~DBConnectionManager()
{
    if(1)
    {
        map<string,MongoDBConnection* >::iterator iter=
                mapName2Connect.begin();
        while(iter!=mapName2Connect.end())
        {
            ++iter;
        }
    }
}
DBConnectionManager* DBConnectionManager::pInstance=NULL;
DBConnectionManager *DBConnectionManager::getInstance()
{
    if(NULL==pInstance)
    {
        pInstance=new DBConnectionManager();
    }
    return pInstance;
}

int DBConnectionManager::addConnection(MongoDBConnection *connect)
{
    map<string,DBConnection<DBClientConnection*,BSONObj>* >::iterator iter=
            mapName2Connect.find(connect->connectName);
    if(iter!=mapName2Connect.end())
    {
        //already exisist
        return -1;
    }
    mapName2Connect.insert(make_pair(connect->connectName,connect));
    return 1;
}

int DBConnectionManager::addConnection(string connectName, DBClientConnection *connect)
{
    MongoDBConnection* conn=
            new MongoDBConnection(connect,connectName);
    int ret= addConnection(conn);
    if(ret<=0)
    {
        delete conn;
        conn=NULL;
        return -1;
    }
    return 1;
}

MongoDBConnection* DBConnectionManager::getConnectByName(string name)
{
    map<string,MongoDBConnection* >::iterator iter=
            mapName2Connect.find(name);
    if(iter!=mapName2Connect.end())
    {
        //already exisist
        return iter->second;
    }
    return NULL;
}

